package br.com.massai.model.entity;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Embeddable;
import javax.persistence.Id;
import javax.persistence.Transient;

@Embeddable
public class GenericEntity implements Serializable {

	private static final long serialVersionUID = 3433955329081207690L;

	public static final int SCALE_VALUE = 2;
	public static final int SCALE_PERCENTAGE = 4;

	@Transient
	private StringBuilder clauseWhere;
	
	@Transient
	private Map<String, Object> parameters;
	
	protected enum ConditionType {
		AND, OR;
	}
	
	@Override
	public String toString() {
		StringBuilder str = new StringBuilder("[").append(this.getClass().getSimpleName()).append("] ");
		try{
			Field[] fields = this.getClass().getDeclaredFields();
			for (Field field : fields) {
				if ((field.getAnnotation(Id.class)) != null) {
					field.setAccessible(true);
					str.append("ID: ").append(field.get(this));
				}
			}
		} catch (IllegalArgumentException e) {
			e.printStackTrace();

		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return str.toString();
	}
	
	protected void addClauseWhere(String clause, ConditionType conditionType) {
		if(this.clauseWhere == null)
			this.clauseWhere = new StringBuilder("");
		this.clauseWhere.append(" ").append(conditionType).append(" ").append(clause);
	}
	
	protected void clearParameters() {
		this.clauseWhere = new StringBuilder();
		this.parameters = new HashMap<String, Object>();
	}

	public StringBuilder getClauseWhere(){
		return this.clauseWhere;
	}

	protected void addParameters(String key, Object value) {
		if(this.parameters == null)
			this.parameters = new HashMap<String, Object>();
		this.parameters.put(key, value);
	}

	public Map<String, Object> getParameters(){
		return this.parameters;
	}
}
